<!--确认起租-->
<template>
	<div class="rent pending-task"
       v-loading="loading"
       element-loading-customClass="ne-loading"
       element-loading-background="rgba(255, 255, 255, 0.6)"
  >
		<el-form ref="elForm" :model="fmodel" status-icon :rules="rules" label-width="1.5rem">
			<el-form-item label="起租/退租日期:" prop="a">
				<el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="fmodel.a"></el-date-picker>
			</el-form-item>
			<el-form-item label="原因:" prop="b">
				<el-select placeholder="请选择" v-model="fmodel.b">
					<el-option v-for="item in rentReasonOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="起租/退租系数:">
				<el-select placeholder="请选择" v-model="fmodel.c">
					<el-option v-for="item in rentCoefficientOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
      <el-form-item label="报竣完成时间:">
        <el-date-picker readonly value-format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="finishTime"></el-date-picker>
      </el-form-item>
			<el-form-item label="备注:" prop="d">
				<el-input type="textarea" v-model="fmodel.d" :rows="2" placeholder="请输入内容"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="validate">确定</el-button>
				<el-button @click="close">取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
import CustOrderService from "@/api/order/CustOrderService";
import { mapState } from "vuex";
import taskProInit from "@/mixins/custorder/taskProInit";
export default {
  name: "Rent",
  mixins: [taskProInit],
  data() {
    return {
      rules: {
        'a': [
          {required: true, message: '请选择日期', trigger: 'change'}
        ],
        'b': [
          {required: false, message: '请填写起租原因', trigger: 'change'},
        ],
        'd': [
          {required: false, message: '请填写备注', trigger: 'change'},
          {required: false, message: '请填写备注', trigger: 'blur'}
        ],
      },
      loading: false,
      rentReasonOptions: [
        {value: 50070004, label: "国内其他运营商原因"},
        {value: 50070007, label: "用户原因(用户自维线路原因)"},
        {value: 50070010, label: "用户原因(用户机房无法使用)"},
        {value: 50070001, label: "端局原因"},
        {value: 50070002, label: "骨干网原因"},
        {value: 50070003, label: "网通原因"},
        {value: 50070005, label: "国际运营商原因"},
        {value: 50070006, label: "用户原因(用户接入设备不到位)"},
        {value: 50070008, label: "用户原因(用户要求延期)"},
        {value: 50070009, label: "用户原因(用户要求退单)"},
        {value: 50070011, label: "用户原因(用户其他原因)"},
        {value: 50070012, label: "其他原因"},
      ],
      rentCoefficientOptions: [
        {value: 30020001, label: "按天计费"},
        {value: 30020003, label: "收半月"},
        {value: 30020004, label: "免收"},
        {value: 30020002, label: "收全月"},
      ],
      fmodel: {
        a: "",
        b: "",
        c: "",
        d: "",
      },
      finishTime: ''
    };
  },
  props: {
    ODKInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    CTOInfo: {
      type: [Object, Array],
      default: () => {
        return {};
      },
    },
  },
  mounted() {},
  created() {
    console.log(this.ODKInfo);
    this.getBjDate();
  },
  computed: {
    ...mapState('orders', ['orderPosInfo']),
    ...mapState('user', ['user'])
  },
  components: {},
  methods: {
    delDHFun(str){
      let str2 = null
      if(str.substring(str.length-1,str.length)==","){
        str2=str.substring(0,str.length-1);
      }
      return str2;
    },
    async getBjDate() {
      let {orderItemId, custId} = this.CTOInfos[0],
          params = {
            interFaceType: "ORDER",
            paramStr: "orderItemId:" + orderItemId + ";custId:" + custId,
            sqlId: 2021071608
          };
      const result = await CustOrderService.getExecuteSqlList(params);
      if (result.meta.resCode == '0') {
        if (result.meta.resList.executeSqlResps) {
          this.finishTime = result.meta.resList.executeSqlResps[0].finishTime;
        }
      } else {
        this.$message({
          showClose: true,
          message: result.meta.resDesc,
          type: 'error'
        });
      }

    },
    validate() {
      this.$refs['elForm'].validate((valid) => {

        if (valid) {
          // 成功
          this.confirm();
        } else {

          return false;
        }
      });
    },
    close() {
      this.$emit('update:show', false)
    },
    async confirm() {
      if (this.finishTime == '') {
        this.$confirm('报竣时间为空，请检查数据！', "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning",
          center: true,
        });
        return false;
      }
      this.loading = true;
      let base = {
        userId: this.user.staffId,
        // orgId: this.user.orgId,
        // orderTaskId: 1,
        // actType: 'MOD',
        oneOrmany: 'one'
      };
      let param = {
        billQuoCd: this.fmodel.c,
        billDate: this.fmodel.a,
        billResultTypeCd: this.fmodel.b,
        billResult: this.fmodel.d
      };
      let {custSoNumber, custId, orderAuditId} = this.CTOInfos[0],
          {taskId} = this.ODKInfo;
      let orderItemIds='';
      this.CTOInfos.map((item,index)=>{
        orderItemIds+=item.orderItemId+',';
      });
      orderItemIds = this.delDHFun(orderItemIds);
      if(!taskId){
        taskId = this.ODKInfo.FUNC_CODE;
      }
      let allParam = {custSoNumber, custId, taskId, orderAuditId, orderItemIds, ...base, ...param};
      const result = await CustOrderService.saveConfirmRent(allParam);
      this.loading = false;
      try {
        if (result.meta.resultCode == 0) {
          this.$message({
            type: 'success',
            message: '保存成功!'
          });
          this.$emit('update:show', false);
        } else {
          this.$message({
            showClose: true,
            message: result.meta.resultMsg,
            type: 'error'
          });
        }
      } catch {
        this.$message({
          showClose: true,
          message: 'code:' + result.code + '  msg:' + result.msg,
          type: 'error'
        });
      }
      this.$emit('refreshList','');
      this.$emit('update:show', false)
    },
    dateCompareFun(date1, date2) {
      date1 = date1.replace(/\-/gi, "/");
      date2 = date2.replace(/\-/gi, "/");
      var time1 = new Date(date1).getTime();
      var time2 = new Date(date2).getTime();
      if (time1 > time2) {
        return true
      } else {
        return false
      }
    },
  },
  watch: {
    'fmodel.a': {
      handler(newName) {
        let comData = this.dateCompareFun(newName, this.finishTime);
        if (comData) {
          this.rules.b[0].required = true;
        } else {
          this.rules.b[0].required = false;
        }
      }
    },
    'fmodel.b': {
      handler(newName) {
        if (newName == 50070012) {
          // 其他原因
          this.rules.d[0].required = true;
          this.rules.d[1].required = true;
        } else {
          this.rules.d[0].required = false;
          this.rules.d[1].required = false;
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
